#include "effect.h"
#include "map.h"
#include "main.h"
#include "player.h"
#include "action.h"
#include "resources/resources.h"
#include "draw/dark_blit.h"

void effect_init_weapon (Effect* effect, BITMAP* sheet, Direction dir, Coord where)
{
	effect->type = EFFECT_STRIKE;
	effect->coord = where;
	effect->origin = where;
	effect->offset_x = 0;
	effect->offset_y = 0;
	effect->cooldown = EFFECT_COOLDOWN_ATTACK;
	effect->dir = dir;
	
	effect->bitmap.source = sheet;
	effect->bitmap.left = 0;
	effect->bitmap.top = 0;
	effect->bitmap.width = 64;
	effect->bitmap.height = 64;
	effect->bitmap.frame_w = 64;
	anim_init (&effect->bitmap.anim, 3, effect->cooldown/3, false);
}

void effect_init_damage (Effect* effect, int amount, Coord where)
{
	effect->type = EFFECT_TEXT;
	effect->coord = where;
	effect->origin = where;
	effect->offset_x = 0;
	effect->offset_y = 0;
	effect->cooldown = EFFECT_COOLDOWN_DAMAGE;
	effect->dir = DIR_NONE;

	effect->text.font = fnt.damage;
	sprintf (effect->text.string, "%d", amount);
}

void effect_init_gold (Effect* effect, int amount, Coord where)
{
	effect->type = EFFECT_TEXT;
	effect->coord = where;
	effect->origin = where;
	effect->offset_x = 0;
	effect->offset_y = 0;
	effect->cooldown = EFFECT_COOLDOWN_GOLD;
	effect->dir = DIR_NONE;
	
	effect->text.font = fnt.sans;
	sprintf (effect->text.string, "%d", amount);
}

void effect_update (Effect* effect)
{
	if (effect->cooldown > 0) {
		effect->cooldown--;
		if (effect->type != EFFECT_TEXT) {
			anim_update (&effect->bitmap.anim);
		}
	}
}

/**
 * Draw background effect graphics.
 * These effects will appear behind other graphics such as the player,
 * map decorations etc.
 * Single effects may have background and foreground portions.
 * Therefore, draw_bg and draw_fg are usually both called on one single
 * effect in one draw cycle.
 */
void effect_draw_bg (Effect* effect, BITMAP* target, int xoffset, int yoffset, int darkness)
{
	int tx, ty;
	int sx, sy;
	
	switch (effect->type) {

	case EFFECT_BITMAP:
		break;
		
	case EFFECT_TEXT:
		break;
		
	case EFFECT_STRIKE:
		tx = xoffset + effect->coord.x * TILE_SIZE + TILE_SIZE/2 - SPRITE_W/2 + effect->offset_x;
		ty = yoffset + effect->coord.y * TILE_SIZE + TILE_SIZE/2 - SPRITE_SIZE/2 - (SPRITE_H-SPRITE_SIZE) + effect->offset_y;
		sx = effect->bitmap.left + effect->bitmap.width/2 - SPRITE_W/2;
		sy = effect->bitmap.height * (effect->dir-1) + effect->bitmap.height/2 - SPRITE_SIZE/2 - (SPRITE_H-SPRITE_SIZE);
		dark_blit (effect->bitmap.source, target, sx, sy, tx, ty, SPRITE_W, SPRITE_H, darkness);
		break;
		
	}
}

/**
 * Draw foreground effect graphics.
 * These effects appear in front of all other map objects.
 */
void effect_draw_fg (Effect* effect, BITMAP* target, int xoffset, int yoffset, int darkness)
{
	int tx, ty;
	int txa[3], tya[3], sxa[3], sya[3];
	
	switch (effect->type) {

	case EFFECT_BITMAP:
		tx = xoffset + effect->coord.x * TILE_SIZE + TILE_SIZE/2 - effect->bitmap.width/2 + effect->offset_x;
		ty = yoffset + effect->coord.y * TILE_SIZE + TILE_SIZE/2 - effect->bitmap.height/2 + effect->offset_y;
		dark_blit (effect->bitmap.source, target, effect->bitmap.left, effect->bitmap.top, tx, ty, effect->bitmap.width, effect->bitmap.height, darkness);
		break;
		
	case EFFECT_TEXT:
		tx = xoffset + effect->coord.x * TILE_SIZE + effect->offset_x;
		ty = yoffset + effect->coord.y * TILE_SIZE + effect->offset_y - (EFFECT_COOLDOWN_DAMAGE - effect->cooldown);
		clear_to_color (bmp.strtemp, makeacol (0,0,0,0));
		
		textout_ex(buffer, effect->text.font, effect->text.string, tx, ty, -1, -1);
			//makeacol (255, 0, 0, effect->cooldown * 255 / EFFECT_COOLDOWN_DAMAGE), -1);
		//drawing_mode (DRAW_MODE_TRANS, NULL, 0, 0);
		//draw_trans_sprite (buffer, bmp.strtemp, tx, ty);
		//drawing_mode (DRAW_MODE_SOLID, NULL, 0, 0);
		break;
		
	case EFFECT_STRIKE:
		// Drawing STRIKE foreground effects:
		// We need to draw everything around the player sprite, so the 
		// effect bitmap is split into 4 parts:
		//  +--------------+
		//  |       1      |
		//  +---+------+---+
		//  | 2 |SPRITE| 3 |
		//  +---+------+---+
		//  |       4      |
		//  +--------------+
		// Note: The lower 8 pixels of the player sprite 
		//       (i.e. its feet) are considered part of area 4.
		// The part of the effect that overlaps with the player sprite 
		// is drawn in effect_draw_bg.
		txa[0] = xoffset + effect->coord.x * TILE_SIZE + TILE_SIZE/2 - effect->bitmap.width/2 + effect->offset_x;
		txa[1] = xoffset + effect->coord.x * TILE_SIZE + TILE_SIZE/2 - SPRITE_W/2 + effect->offset_x;
		txa[2] = xoffset + effect->coord.x * TILE_SIZE + TILE_SIZE/2 + SPRITE_W/2 + effect->offset_x;
		tya[0] = yoffset + effect->coord.y * TILE_SIZE + TILE_SIZE/2 - effect->bitmap.height/2 + effect->offset_y;
		tya[1] = yoffset + effect->coord.y * TILE_SIZE + TILE_SIZE/2 - SPRITE_SIZE/2 - (SPRITE_H-SPRITE_SIZE) + effect->offset_y;
		tya[2] = yoffset + effect->coord.y * TILE_SIZE + TILE_SIZE/2 + effect->offset_y;
		sxa[0] = effect->bitmap.left;
		sxa[1] = effect->bitmap.left + effect->bitmap.width/2 - SPRITE_W/2;
		sxa[2] = effect->bitmap.left + effect->bitmap.width/2 + SPRITE_W/2;
		sya[0] = effect->bitmap.height * (effect->dir-1);
		sya[1] = effect->bitmap.height * (effect->dir-1) + effect->bitmap.height/2 - SPRITE_SIZE/2 - (SPRITE_H-SPRITE_SIZE);
		sya[2] = effect->bitmap.height * (effect->dir-1) + effect->bitmap.height/2;
		
		dark_blit (effect->bitmap.source, target, sxa[0], sya[0], txa[0], tya[0], effect->bitmap.width, sya[1] - sya[0], darkness);
		dark_blit (effect->bitmap.source, target, sxa[0], sya[1], txa[0], tya[1], sxa[1] - sxa[0], SPRITE_H, darkness);
		dark_blit (effect->bitmap.source, target, sxa[2], sya[1], txa[2], tya[1], effect->bitmap.width - (sxa[2] - effect->bitmap.left), SPRITE_H, darkness);
		dark_blit (effect->bitmap.source, target, sxa[0], sya[2], txa[0], tya[2], effect->bitmap.width, effect->bitmap.height - (sya[2] - effect->bitmap.height * (effect->dir-1)), darkness);
		break;
		
	}
}
